<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>用于拦截 target 对象上的 propKey 的赋值操作。如果目标对象自身的某个属性，不可写且不可配置，那么set方法将不起作用</p>
</body>
<script>
    let validator = {
        set: function(obj, prop, value) {
            if (prop === 'age') {
                if(!Number.isInteger(value)) {
                    throw new TypeError(' The age is not an integer');
                }
                if (value > 200) {
                    throw new RangeError('The age seems invalid');
                }
            }
            // 对于满足条件的 age 属性以及其他属性，直接保存
            obj[prop] = value;
        }
    };
    let proxy = new Proxy({}, validator)
    proxy.age = 100;
    proxy.age   //100
    proxy.age = 'oppps' // 报错
    proxy.age = 300     // 报错
</script>
</html>